#!/bin/sh
if [ ! "$1" ] 
	then mysql_port=3306 
else 
	mysql_port=$1
fi
mysql_tar_suffix="tar.gz"
DATASONG_HOME=/home/datasong/release
####################check if mysql installed#####################
mysql_install_path=${DATASONG_HOME}/plugin/mysql
mysql_tar_path=${DATASONG_HOME}/resource/mysql/

sql_file=${DATASONG_HOME}/resource/mysql/datasong.sql
echo "DATASONG_HOME=${DATASONG_HOME}"
if [ ! -d "${mysql_install_path}" ]; then #check if mysql folder exists in install path , if not, install 
    mysql_tar_file=`find ${DATASONG_HOME}/resource/mysql -name "*.${mysql_tar_suffix}"`
    for f in ${mysql_tar_file}
    do
        tar -zxf ${mysql_tar_file} -C ${mysql_tar_path} & #extract tar file in current path, turn to background
		b=''
		while true
		do
			printf "MySQL is installing:%-50s\r" $b 
			sleep 1
			b=#$b
			if [[ ! $(ps -ef | grep tar) =~ "${mysql_tar_path}" ]]; then  
				echo ""
				echo "MySQL install successful!"
				break
			fi  
		done
		
        mysql_install_folder=`echo ${mysql_tar_file} | sed 's/.tar.gz//g'` #delete suffix(.tar.gz) in folder name
        mv ${mysql_install_folder} ${mysql_install_path} #move mysql folder to install path 
		cp ${sql_file} ${mysql_install_path} #copy init .sql file to install path 
    done
	
	#sql_dst_file=${mysql_install_path}/datasong.sql 

	
    mysql_group=mysql
    mysql_user=mysql
    egrep "${mysql_group}" /etc/group >& /dev/null
    if [ $? -ne 0 ]
    then
        groupadd ${mysql_group}
    else
        echo "Group ${mysql_group} has already exist!"
    fi
 
    egrep "${mysql_user}" /etc/passwd >& /dev/null
    if [ $? -ne 0 ]
    then
        useradd -r -g ${mysql_group} ${mysql_user}
    else
        echo "User ${mysql_user} has already exist!"
    fi
	#mkdir ${mysql_install_path}/mysql-files
	#mkdir ${mysql_install_path}/data

    chown -R ${mysql_group}:${mysql_user} ${mysql_install_path}
	chown -R root ${mysql_install_path}
	chmod -R 777 ${mysql_install_path}

    ${mysql_install_path}/scripts/mysql_install_db --basedir=${mysql_install_path} --datadir=${mysql_install_path}/data --socket=${mysql_install_path}/mysql.sock --port=${mysql_port} --user=mysql  >& /dev/null

    ${mysql_install_path}/bin/mysqld --initialize-insecure --basedir=${mysql_install_path} --datadir=${mysql_install_path}/data --socket=${mysql_install_path}/mysql.sock --port=${mysql_port} --user=mysql  >& /dev/null
    ${mysql_install_path}/bin/mysql_ssl_rsa_setup --datadir=${mysql_install_path}/data --port=${mysql_port} >& /dev/null
	${mysql_install_path}/bin/mysqld --basedir=${mysql_install_path} --datadir=${mysql_install_path}/data --socket=${mysql_install_path}/mysql.sock --port=${mysql_port} --user=mysql & 
	
	b=''
    while true
    do
        printf "MySQL is starting:%-50s\r" $b 
        sleep 1
        b=#$b
        if [[ $(ps aux | grep mysql) =~ "--basedir=${mysql_install_path}" ]]; then  
            echo ""
            echo "MySQL start successful!"
            break
        fi  
    done
	

    mysql_login="root"
    mysql_pass="" 
    new_user_name="datasong"
    new_user_pass="iscas123"
    sql_createuser="CREATE USER '${new_user_name}' IDENTIFIED BY '${new_user_pass}';";
    sql_grant_local="GRANT ALL PRIVILEGES ON *.* TO '${new_user_name}'@'localhost' IDENTIFIED BY '${new_user_pass}' WITH GRANT OPTION;";
    sql_grant_remote="GRANT ALL PRIVILEGES ON *.* TO '${new_user_name}'@'%' IDENTIFIED BY '${new_user_pass}' WITH GRANT OPTION;";
    sql_flush="FLUSH PRIVILEGES;";
    sql_add="${sql_createuser}${sql_grant_remote}${sql_grant_local}${sql_flush}";
    ${mysql_install_path}/bin/mysql --default-character-set=utf8 --socket=${mysql_install_path}/mysql.sock --user=${mysql_login} --password=${mysql_pass} --port=${mysql_port} --execute="$sql_add";
	
    ${mysql_install_path}/bin/mysql --default-character-set=utf8 --socket=${mysql_install_path}/mysql.sock --user=$new_user_name --password=$new_user_pass --port=${mysql_port}  < ${mysql_install_path}/datasong.sql &
     sleep 10
fi

####################check if mysql running####################
if [[ $(ps aux | grep mysql) =~ "--basedir=${mysql_install_path}" ]]; then  
    echo "MySQL is running"
else
    ${mysql_install_path}/bin/mysqld --basedir=${mysql_install_path} --datadir=${mysql_install_path}/data --socket=${mysql_install_path}/mysql.sock --port=${mysql_port} --user=mysql &
	b=''
    while true
    do
        printf "MySQL is starting:%-50s\r" $b 
        sleep 1
        b=#$b
        if [[ $(ps aux | grep mysql) =~ "--basedir=${mysql_install_path}" ]]; then  
            echo ""
            echo "MySQL is start successful!"
            break
        fi  
    done
fi 